import 'package:edu/constants/app_strings.dart';
import 'package:edu/pages/course/course_detail/tab/course_brief/course_brief.dart';
import 'package:edu/pages/main_tab/edu/edu_course/edu_course_page_logic.dart';
import 'package:edu/pages/main_tab/edu/edu_course/edu_course_tab/edu_course_review_widget.dart';
import 'package:edu/pages/main_tab/edu/edu_course/edu_course_tab/edu_course_table_contents_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class EduCourseTabWidget extends StatefulWidget {
  const EduCourseTabWidget({super.key});

  @override
  State<EduCourseTabWidget> createState() => _EduCourseTabWidgetState();
}

class _EduCourseTabWidgetState extends State<EduCourseTabWidget> {
  late List<Widget> _tabs;

  @override
  void initState() {
    super.initState();
    _tabs = [
      _buildTabCatalog(context),
      _buildTabBrief(context),
      _buildTabComment(context),
    ];
  }

  // 构建课程目录
  Widget _buildTabCatalog(BuildContext context) {
    return Selector<EduCoursePageLogic, bool>(
      selector: (context, logic) => logic.isContentEmpty,
      builder: (context, v, child) {
        if (v) {
          return const SizedBox.shrink();
        } else {
          return EduCourseTableContentsWidget();
        }
      },
    );
  }

  // 构建课程简介
  Widget _buildTabBrief(BuildContext context) {
    return Selector<EduCoursePageLogic, bool>(
      selector: (context, logic) => logic.isContentEmpty,
      builder: (context, v, child) {
        if (v) {
          return const SizedBox.shrink();
        } else {
          final logic = context.read<EduCoursePageLogic>();
          return CourseBrief(courseDetail: logic.courseDetail!);
        }
      },
    );
  }

  // 构建课程评价
  Widget _buildTabComment(BuildContext context) {
    return Selector<EduCoursePageLogic, bool>(
      selector: (context, logic) => logic.isContentEmpty,
      builder: (context, v, child) {
        if (v) {
          return const SizedBox.shrink();
        } else {
          return EduCourseReviewWidget();
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: DefaultTabController(
        length: _tabs.length,
        initialIndex: context.read<EduCoursePageLogic>().currentTabIndex,
        child: Column(
          children: [_buildTabBar(context), _buildTabBody(context)],
        ),
      ),
    );
  }

  // 构建TabBar
  Widget _buildTabBar(BuildContext context) {
    final bkColor = Theme.of(context).colorScheme.surface;
    return Container(
      height: 36,
      width: double.infinity,
      color: bkColor,
      child: TabBar(
        dividerHeight: 0.5,
        dividerColor: bkColor,
        isScrollable: true,
        tabAlignment: TabAlignment.center,
        labelPadding: const EdgeInsets.symmetric(horizontal: 20),
        labelStyle: const TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
        unselectedLabelStyle: const TextStyle(fontSize: 17),
        tabs: [
          Tab(text: AppStrings.courseCatalog),
          Tab(text: AppStrings.courseBrief),
          Tab(text: AppStrings.courseComment),
        ],
      ),
    );
  }

  // 构建TabBody
  Widget _buildTabBody(BuildContext context) {
    return Selector<EduCoursePageLogic, int>(
      selector: (context, logic) => logic.currentTabIndex,
      builder: (context, index, child) {
        final logic = context.read<EduCoursePageLogic>();
        final controller = DefaultTabController.of(context);
        controller.addListener(() {
          if (controller.index != logic.currentTabIndex) {
            logic.changeTabIndex(controller.index);
          }
        });
        return Expanded(
          child: SafeArea(child: TabBarView(children: _tabs)),
        );
      },
    );
  }
}
